package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("cargo/contract")
public class ContractController extends BaseController{

    @Reference //dubbo注解
    ContractService contractService;

    @RequestMapping(value="list",name = "购销合同列表")
    public String findAll(
            @RequestParam(defaultValue = "1")int page,
            @RequestParam(defaultValue = "10")int size){

        //查询当前企业的合同列表
        ContractExample contractExample = new ContractExample();
        ContractExample.Criteria criteria = contractExample.createCriteria();
        criteria.andCompanyIdEqualTo(getCompanyId());

        //继续加条件 根据degree的不同,展示不同的数据
        User loginUser = getLoginUser();
        if (loginUser.getDegree() == 4) {
            //若是普通员工,根据创建者id进行查询
            criteria.andCreateByEqualTo(loginUser.getId());
        }else if(loginUser.getDegree() == 3){
            //若是管理本部门人员,根据部门的id进行查询
            criteria.andCreateDeptEqualTo(loginUser.getDeptId());
        }//...
        //其他人就查询当前公司的所有数据即可

        //排序
        contractExample.setOrderByClause("create_time desc");

        //查询操作
        PageInfo pageInfo = contractService.findAll(page, size, contractExample);
        request.setAttribute("page",pageInfo);

        return "cargo/contract/contract-list";
    }

    @RequestMapping(value = "toAdd",name = "跳转到合同添加页面")
    public String toAdd(){
        //不异步了
        //这里直接丢 一个list出去参考User
        List<User> userList = contractService.findName("购销合同");
        //request 丢
        request.setAttribute("userList",userList);
        return "cargo/contract/contract-add";
    }

    @RequestMapping(value = "toUpdate",name = "跳转到合同更新页面")
    public String toUpdate(String id){

        Contract contract = contractService.findById(id);
        request.setAttribute("contract",contract);

        return "cargo/contract/contract-update";
    }

    @RequestMapping(value = "edit",name = "合同添加或者更新")
    public String edit(Contract contract){

        if (StringUtils.isNotBlank(contract.getId())) {
            //更新
            contractService.update(contract);

        }else {
            //添加
            //设置合同的状态
            contract.setState(0);

            //设置合同创建人,创建部门,公司id
            contract.setCreateBy(getLoginUser().getId());
            contract.setCreateDept(getLoginUser().getDeptId());

            contract.setCompanyId(getCompanyId());
            contract.setCompanyName(getCompanyName());

            contractService.save(contract);

        }

        return "redirect:/cargo/contract/list.do";
    }

    @RequestMapping(value = "delete",name = "删除合同")
    public String deletById(String id){
        contractService.deleteById(id);
        return "redirect:/cargo/contract/list.do";
    }

    //合同的提交操作  0->1
    @RequestMapping(value = "submit",name = "提交合同")
    public String submit(String id){
        Contract contract = new Contract();
        contract.setId(id);
        contract.setState(5);

        contractService.update(contract);
        return "redirect:/cargo/contract/list.do";
    }

    //合同的取消操作  1->0
    @RequestMapping(value = "cancel",name = "取消合同")
    public String cancel(String id){
        Contract contract = new Contract();
        contract.setId(id);
        contract.setState(0);

        contractService.update(contract);
        return "redirect:/cargo/contract/list.do";
    }
}
